package com.zjsru.oneDay202210;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @Author: likew
 * @Date: 2022/10/4
 *  使括号有效的最少添加
 *
 *  输入：s = "())"
 * 输出：1
 *
 * 输入：s = "((("
 * 输出：3
 */
public class MinAddToMakeValid {
    
    public int minAddToMakeValid(String s) {
        int ans = 0, score = 0;
        for (char c : s.toCharArray()) {
            score += c == '(' ? 1 : -1;
            if (score < 0) {
                score = 0;
                ans++;
            }
        }
        return ans + score;
    }
    
    public static void main(String[] args) {
        MinAddToMakeValid minAddToMakeValid = new MinAddToMakeValid();
        String s = "()))((";
        System.out.println(minAddToMakeValid.minAddToMakeValid(s));
    }
}
